package org.example.admin.aircraft;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class addAircraftView extends JFrame {
    private JTextField aircraftIdField,aircraftModelField,seatsField;
    private JPasswordField passwordField;
    private JButton addButton;

    public addAircraftView() {
        setTitle("管理端飞机信息添加页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(8, 2));

        JLabel aircraftIdLabel = new JLabel("飞机id");
        aircraftIdField = new JTextField();
        JLabel aircraftModelLabel = new JLabel("飞机型号");
        aircraftModelField = new JPasswordField();
        JLabel seatsLabel = new JLabel("飞机座位");
        seatsField = new JTextField();
        addButton = new JButton("添加");

        // 添加组件到窗口
        add(aircraftIdLabel);
        add(aircraftIdField);
        add(aircraftModelLabel);
        add(aircraftModelField);
        add(seatsLabel);
        add(seatsField);
        add(new JLabel()); // 占位
        add(addButton);

        // 添加添加按钮的点击事件监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addUserInfoToDatabase();
            }
        });
    }

    private void addUserInfoToDatabase() {
        String aircraftId = aircraftIdField.getText();
        String aircraftModel = aircraftModelField.getText();
        String seats = seatsField.getText();

        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建插入语句
            String insertQuery = "INSERT INTO air_aircraft (aircraft_id, model, seats) VALUES (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(insertQuery);
            statement.setString(1, aircraftId);
            statement.setString(2, aircraftModel);
            statement.setString(3, seats);

            // 执行插入操作
            statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            // 提示添加成功
            JOptionPane.showMessageDialog(this, "添加成功");

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "添加失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        aircraftIdField.setText("");
        aircraftModelField.setText("");
        seatsField.setText("");
    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            addAircraftView addAircraftForm = new addAircraftView();
//            addAircraftForm.setSize(300, 200);
//            addAircraftForm.setVisible(true);
//        });
    }
}